package com.gsit.job.services;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gsit.job.domain.Person;


@Repository("personService")
@Qualifier("personService")
public class PersonService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public List<Person> findAll() {
		return entityManager.createNamedQuery("Person.findAll",Person.class).getResultList();
	}
	
	
	@Transactional
	public List<Person> findByResumeCaptured(Boolean isResumeCaptured) {
		Query query = entityManager.createQuery("select person from Person person where person.isResumeCaptured=:isResumeCaptured ",Person.class);
		query.setParameter("isResumeCaptured", isResumeCaptured);
		query.setMaxResults(10000);
		return query.getResultList();
	}

	@Transactional
	public <T> T find(Class<T> entityClass, Long id) {
		return entityManager.find(entityClass, id);
	}

	@Transactional
	public Person saveOrUpdate(Person entity) {
		try{
			entityManager.merge(entity);
		}catch(Exception exception){
		}
		return entity;
	}
	
	@Transactional
	public List<Person> saveOrUpdate(List<Person> entities) {
		for (Person person : entities) {			
			saveOrUpdate(person);
		}
		return entities;
	}

	@Transactional
	public void delete(Person entity) {
		entityManager.remove(entityManager.merge(entity));
	}
	
	
}
